package com.ilop.sthome.ui.activity.device;

import android.util.Log;
import android.view.View;

import com.ilop.sthome.ui.activity.main.ContainLoginActivity;
import com.example.common.base.OnCallBackToRefresh;
import com.example.common.utils.LiveDataBus;
import com.example.common.view.refresh.CustomRefreshView;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.bean.DeviceHistoryBean;
import com.ilop.sthome.data.event.EventAnswerOK;
import com.ilop.sthome.data.event.EventRefreshSubLogs;
import com.ilop.sthome.mvp.contract.device.DeviceHistoryContract;
import com.ilop.sthome.mvp.present.device.DeviceHistoryPresenter;
import com.ilop.sthome.common.command.SiterCommand;
import com.ilop.sthome.ui.adapter.device.DeviceHistoryAdapter;
import com.ilop.sthome.ui.dialog.BaseDialog;
import com.siterwell.familywellplus.R;
import com.siterwell.familywellplus.databinding.ActivityDeviceHistroyBinding;

import java.util.List;

/**
 * @author skygge
 * @date 2019-12-10.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：子设备报警/历史详情
 */
public class DeviceHistoryActivity extends ContainLoginActivity<DeviceHistoryPresenter, ActivityDeviceHistroyBinding> implements DeviceHistoryContract.IView {

    private int mPage = 1;
    private boolean isEnd = false;

    private int mDeviceId;
    private String mDeviceName;
    private DeviceHistoryAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_histroy;
    }

    @Override
    protected void initialize() {
        super.initialize();
        mDeviceName = getIntent().getStringExtra(CommonId.KEY_DEVICE_NAME);
        mDeviceId = getIntent().getIntExtra(CommonId.KEY_DEVICE_ID,-1);
    }

    @Override
    protected void initPresent() {
        super.initPresent();
        mPresent = new DeviceHistoryPresenter(mContext);
    }

    @Override
    protected void initView() {
        super.initView();
        mAdapter = new DeviceHistoryAdapter(mContext);
        mDBind.rvHistoryList.setAdapter(mAdapter);
        mDBind.rvHistoryList.setLoadMoreEnable(true);
        withoutData();
    }

    @Override
    protected void initData() {
        super.initData();
        mPresent.getDeviceInfo(mDeviceName, mDeviceId);
        mPresent.syncSubAlarm(1, mDeviceId);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.clear.setOnClickListener(view -> alertToDelete());
        mDBind.ivHistoryBack.setOnClickListener(view -> finish());
        mDBind.rvHistoryList.setOnLoadListener(new CustomRefreshView.OnLoadListener() {
            @Override
            public void onRefresh() {
                mPresent.syncSubAlarm(1, mDeviceId);
                mDBind.rvHistoryList.complete();
            }

            @Override
            public void onLoadMore() {
                if(!isEnd) mPresent.syncSubAlarm(mPage, mDeviceId);
                mDBind.rvHistoryList.complete();
                mDBind.rvHistoryList.onLoadingMore();
            }
        });
    }

    @Override
    protected void initDataBus() {
        super.initDataBus();
        LiveDataBus.get().with(CommonId.KEY_ANSWER_OK, EventAnswerOK.class).observe(this, event -> {
            dismissProgressDialog();
            if (event!=null){
                Log.i(TAG, "initDataBus: answerOK");
                if (event.getData_str1().length() == 9) {
                    int cmd = Integer.parseInt(event.getData_str1().substring(0, 4), 16);
                    if (cmd == SiterCommand.DELETE_SUBDEVICE_ALARM_LIST) {
                        if (CommonId.KEY_SUCCESS.equals(event.getData_str2())) {
                            withoutData();
                            showToast(getString(R.string.delete_success));
                        }else {
                            showToast(getString(R.string.failed));
                        }
                    }
                }
            }
        });

        LiveDataBus.get().with(CommonId.KEY_EVENT_REFRESH_SUB_LOGS, EventRefreshSubLogs.class).observe(this, event -> {
            if (event != null){
                if (mDeviceId == event.getDeviceId() && mDeviceName.equals(event.getDeviceName())) {
                    if (event.getIsOver() == 1) {
                        mPage = 1;
                        isEnd = true;
                    } else {
                        mPage = event.getPage() + 1;
                        isEnd = false;
                    }
                    if (event.getList()!=null) {
                        new Thread(()->mPresent.getSubHistoryList(event.getList(), event.getPage())).start();
                    }else if (event.getIsOver() == 1 && event.getPage() == 1){
                        withoutData();
                    }
                }
            }
        });
    }

    private void alertToDelete(){
        BaseDialog mDialog = new BaseDialog(mContext, new OnCallBackToRefresh() {
            @Override
            public void onConfirm() {
                showProgressDialog();
                mPresent.deleteSubAlarm(mDeviceId);
            }

            @Override
            public void onCancel() {

            }
        });
        mDialog.setMsg(getString(R.string.delete_or_not));
        mDialog.show();
    }


    @Override
    public void showHistory(List<DeviceHistoryBean> history) {
        mAdapter.setList(history);
        mDBind.clear.setVisibility(View.VISIBLE);
        mDBind.rvHistoryList.onNoMore();
    }

    @Override
    public void withoutData() {
        mAdapter.setList(null);
        mDBind.rvHistoryList.setEmptyView(getString(R.string.no_data), R.mipmap.device_history_empty);
        mDBind.clear.setVisibility(View.INVISIBLE);
    }
}
